package com.lm.activiti.service;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaveProcessService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /** 部署流程 */
    public void deployProcess() {
        repositoryService.createDeployment()
                .addClasspathResource("processes/leave-process.bpmn20.xml")
                .name("请假审批流程")
                .deploy();
        System.out.println("流程部署成功！");
    }

    /** 启动流程实例 */
    public void startProcess() {
        runtimeService.startProcessInstanceByKey("leaveProcess");
        System.out.println("流程实例启动成功！");
    }

    /** 查询并完成任务 */
    public void completeTask(String assignee) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
        for (Task task : tasks) {
            System.out.println("处理任务: " + task.getName());
            taskService.complete(task.getId());
        }
    }
}
